Ext.define('StaffServer.controller.SysRoleController', {
	extend : 'Ext.app.Controller',

	models: ['SysRoleModel', 'SysRoleModel'],
	stores: ['SysRoleStore', 'RoleResourceTreeStore'],
	views:  ['sysRole.SysRoleGrid', 'sysRole.SysRoleMngr', 'sysRole.SysRoleSearchPanel', 'sysRole.SysRoleWindow', 'sysRole.SysRoleForm', 'sysRole.ChooseResourceWindow', 'sysRole.SysRoleReadOnlyForm', 'sysRole.ChooseResourceTree'],
	
	tabView: 'StaffServer.view.sysRole.SysRoleMngr',
	
	sysRoleWindow: null,
	sysRoleForm: null,
	chooseResourceWindow: null,
	submitFlag: null,
	
	refs : [ {
		ref : 'sysRoleSearchPanel',
		selector : 'sysRoleSearchPanel'
	}, {
		ref : 'sysRoleGrid',
		selector : 'sysRoleGrid'
	}, {
		ref : 'sysRoleReadOnlyForm',
		selector : 'sysRoleReadOnlyForm'
	}, {
		ref : 'chooseRoleGrid',
		selector : 'chooseRoleGrid'
	}, {
		ref : 'chooseResourceTree',
		selector : 'chooseResourceTree'
	}],

	
	// 初始化执行语句
	init : function() {
		sysRoleWindow = Ext.widget("sysRoleWindow");
		chooseResourceWindow = Ext.widget("chooseResourceWindow");
		sysRoleForm = Ext.ComponentQuery.query('sysRoleForm')[0];
		// 控制事件
		this.control({
			'sysRoleSearchPanel button[action=search]' : {
				click : this.search
			},
			'sysRoleSearchPanel button[action=reset]' : {
				click : this.reset
			},
			'sysRoleGrid button[action=saveSysRole]' : {
				click : this.saveSysRole
			},
			'sysRoleGrid button[action=updateSysRole]' : {
				click : this.updateSysRole
			},
			'sysRoleGrid button[action=deleteSysRole]' : {
				click : this.deleteSysRole
			},
			'sysRoleGrid button[action=chooseResource]' : {
				click : this.chooseResource
			},
			'sysRoleWindow button[action=submit]' : {
				click : this.submitWindow
			},
			'sysRoleWindow button[action=close]' : {
				click : this.closeWindow
			},
			'sysRoleGrid': {
				itemdblclick: this.updateSysRole
			},
			'chooseResourceWindow button[action=submit]' : {
				click : this.submitChooseResource
			},
			'chooseResourceWindow button[action=close]' : {
				click : this.closeChooseResourceWindow
			}
		});
	},

	//searchPanel查询
	search : function() {
		var searchPanel = this.getSysRoleSearchPanel();
		var grid = this.getSysRoleGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('sysRoleGrid > pagingtoolbar')[0].moveFirst();
	},
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getSysRoleSearchPanel();
		searchPanel.getForm().reset();
	},
	
	//新增
	saveSysRole: function(){
		submitFlag = "save";
		sysRoleWindow.setTitle("新增角色");
		sysRoleWindow.show();
	},
	
	//更新
	updateSysRole: function(){
		var record = this.getSysRoleGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        sysRoleForm.loadRecord(record);
        submitFlag = "update";
        sysRoleWindow.setTitle("修改角色");
        sysRoleWindow.show();
        
	},
	
	//删除
	deleteSysRole: function(){
		var $this = this;
		var record = this.getSysRoleGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'sysRole/deleteSysRole',
                    params: {
                       roleId: record.get("roleId")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.search();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	//提交表单
	submitWindow: function(){
		if (!sysRoleForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = sysRoleForm.getValues();
		var url;
		sysRoleForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: submitFlag == 'save' ? 'sysRole/saveSysRole' : 'sysRole/updateSysRole',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//关闭窗口
	closeWindow: function(){
		sysRoleForm.getForm().reset();
		sysRoleWindow.hide();
	},
	
	//分配角色
	chooseResource: function(){
		var record = this.getSysRoleGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        var resourceTree = this.getChooseResourceTree();
        var treeStore = resourceTree.getStore();
        treeStore.on('beforeload', function(store){
			var roleId = record.get("roleId");
			Ext.apply(store.proxy.extraParams, {roleId: roleId});
        });
        treeStore.load();
        this.getSysRoleReadOnlyForm().loadRecord(record);
		chooseResourceWindow.setTitle("分配权限");
        chooseResourceWindow.show();
	},
	
	
	submitChooseResource: function(){
		var $this = this;
		var jsonArray = new Array();
		var records = this.getChooseResourceTree().getChecked();
        if(records.length == 0){
           Ext.Msg.alert('提示', '请先选择要分配的权限!');
           return;
        }
         for (var i = 0; i < records.length; i++) {
            var record = records[i].data;
            jsonArray.push(record);
        };
        var roleId = this.getSysRoleReadOnlyForm().getRecord().get("roleId");
        showWaitMsg(); 
		Ext.Ajax.request({
	        url: 'sysRole/chooseResource',
	        params: {
	        	roleId: roleId,
	           	resourceTree: Ext.JSON.encode(jsonArray)
	        },
	        success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeChooseResourceWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
	    });
	},
	
	
	closeChooseResourceWindow: function(){
		chooseResourceWindow.hide();
	}
	
	
});